<?php

namespace App\Imports;

use App\Models\User\User;
use Illuminate\Support\Arr;
use Illuminate\Support\Collection;
use Maatwebsite\Excel\Concerns\ToCollection;
use Maatwebsite\Excel\Concerns\WithCalculatedFormulas;
class UserImport implements ToCollection,WithCalculatedFormulas
{

    public $existsPhones = [];


    /**
    * @param Collection $collection
    */
    public function collection(Collection $collection)
    {
        $users = $collection->toArray();
        unset($users[0]);
        unset($users[1]);
        unset($users[2]);

        $phones = User::pluck('phone')->toArray();
        $info = [];
        $time = date('Y-m-d H:i:s');
        foreach ($users as $user) {

            if(in_array($user[8], $phones)){
                $this->existsPhones[] = $user[8];
            }
            $info[] = [
                'id_card'=>$user[4],
                'gender'=>$user[5],
                'age'=>$user[6],
                'phone'=>$user[8],
                'call_phone'=>$user[8],
                'real_name'=>$user[13],
                'created_at'=>$time,
                'updated_at'=>$time,
            ];
        }

        if(count($this->existsPhones)<=0){
            User::insert($info);
        }
    }


}
